<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Categories extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->user_auth->manage();

		$this->load->model('category_model');

		$this->menu_view = $this->menu_lib->get(1);
	}

	public function index()
	{
		//$data = array(
		//	'content' => $this->flat_app->get('index.php')
		//);
		//$this->load->view('template', $data);
		$action = $this->input->get('action');
		$data = array();

		switch($action)
		{
		case 'create':
			if(!empty($_GET['name']))
			{
				$res = $this->category_model->add(array('name' => $_GET['name']));

				if($res)
				{
					$data['text_success'] = 'Categor&iacute;a agregada';
				}
				else
				{
					$data['text_error'] = 'Error al tratar de agregar categor&iacute;a';
				}
			}
			break;
		case 'delete':
			if(!empty($_GET['id']))
			{
				$res = $this->category_model->delete(array('id' => $_GET['id']));
				if($res)
				{
					$data['text_success'] = 'Categor&iacute;a eliminada';
				}
				else
				{
					$data['text_error'] = 'Error al tratar de eliminar categor&iacute;a';
				}
			}
			break;
		case 'update':
			if(!empty($_GET['name'])&&!empty($_GET['id']))
			{
				$res = $this->category_model->update(array('id' => $_GET['id'], 'name' => $_GET['name']));
				if($res)
				{
					$data['text_success'] = 'Categor&iacute;a actualizada';
				}
				else
				{
					$data['text_error'] = 'Error al tratar de actualizar categor&iacute;a';
				}
			}
			break;
		}

		$data['categories'] = $this->category_model->count_books();
		$include_view = $this->load->view('categories', $data, TRUE);

		$this->load->view('template', array('content' => $this->menu_view . $include_view));
	}

	public function add()
	{
		//$_GET['submodule'] = 'new';
		//$this->index();
		$include_view = $this->load->view('categories_new', array(), TRUE);

		$this->load->view('template', array('content' => $this->menu_view . $include_view));
	}

	public function edit()
	{
		//$this->index();
		$data = array(
			'category' => $this->category_model->get(array('id' => $this->input->get('id')))
		);

		if(empty($data['category']))
		{
			$include_view = $this->load->view('categories_404', array(), TRUE);
		}
		else
		{
			$include_view = $this->load->view('categories_edit', $data, TRUE);
		}

		$this->load->view('template', array('content' => $this->menu_view . $include_view));
	}

}

/* End of file categories.php */
/* Location: ./application/controllers/categories.php */
